import { createStore } from 'vuex';
import { listdata, userSignUp, userlogin } from "../ajax/index"
import { usertoken } from "../lib/lib"

export default createStore({
    state: {
        listDataValue: [],  
        token: usertoken.getToken(),
    },
    mutations: {
        GETLISTDATA(state, data) {
            state.listDataValue = data;
        },
        GETUSERLOGIN(state, data) {
            state.token = data;
        }
    },
    actions: {
        async getlistdata({ commit }) {
            let result = await listdata()
            if (result.code == 200) {
                commit('GETLISTDATA', result.data)
            }
        },
        async userRegister(data) {
            let result = await userSignUp(data);
            if (result.code == 201) {
                return result.message;
            } else {
                console.log(result.message);
                return Promise.reject(new Error('faile'));
            }
        },
        async getuserlogin({ commit }, data) {
            let result = await userlogin(data);
            if (result.code == 200) {
                commit('GETUSERLOGIN', result.token);
                usertoken.setToken(result.token);
                return 'successfully'
            } else {
                return Promise.reject(new Error('faile'));
            }
        },
    },
    getters: {

    }
})

